package persentation.presentUniversityDean;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import vo.frameworkVO.FrameworkVO;
import businesslogic.frameworkBl.FrameworkController;
import dataservice.ResultMessage;

import java.awt.Font;

public class KuangjiaceluePanel1 extends JPanel {
	private FrameworkController frameworkController;
	private FrameworkVO frameworkVO;
	private static Image INFO = new ImageIcon(
			"Graphics/universityDean/info.png").getImage();

	/**
	 * Create the panel.
	 */
	public KuangjiaceluePanel1(final FrameworkController frameworkController) {
		setLayout(null);
		this.frameworkController = frameworkController;
		try {
			frameworkVO = frameworkController.find();
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}

		JLabel label = new JLabel("通识通修模块");
		label.setFont(new Font("微软雅黑", Font.BOLD, 18));
		label.setBounds(131, 91, 112, 28);
		add(label);

		JLabel label_1 = new JLabel("核心课程模块");
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 18));
		label_1.setBounds(131, 160, 112, 28);
		add(label_1);

		JLabel label_2 = new JLabel("开放选修模块");
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 18));
		label_2.setBounds(131, 233, 112, 28);
		add(label_2);

		final JSpinner spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(frameworkVO.getCompulsoryLow(),
				0, 50, 1));
		spinner.setToolTipText("输入下限");
		spinner.setFont(new Font("微软雅黑", Font.BOLD, 14));
		spinner.setBounds(340, 91, 78, 28);
		add(spinner);

		final JSpinner spinner_1 = new JSpinner();
		spinner_1.setModel(new SpinnerNumberModel(frameworkVO
				.getCompulsoryHigh(), 0, 50, 1));
		spinner_1.setToolTipText("输入上限");
		spinner_1.setForeground(Color.GRAY);
		spinner_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
		spinner_1.setBounds(520, 91, 78, 28);
		add(spinner_1);

		final JSpinner spinner_2 = new JSpinner();
		spinner_2.setModel(new SpinnerNumberModel(frameworkVO.getCoreLow(), 0,
				50, 1));

		spinner_2.setToolTipText("输入下限");
		spinner_2.setForeground(Color.GRAY);
		spinner_2.setFont(new Font("微软雅黑", Font.BOLD, 14));
		spinner_2.setBounds(340, 160, 78, 28);
		add(spinner_2);

		final JSpinner spinner_3 = new JSpinner();
		spinner_3.setModel(new SpinnerNumberModel(frameworkVO.getCoreHigh(), 0,
				50, 1));
		spinner_3.setToolTipText("输入上限");
		spinner_3.setForeground(Color.GRAY);
		spinner_3.setFont(new Font("微软雅黑", Font.BOLD, 14));
		spinner_3.setBounds(520, 160, 78, 28);
		add(spinner_3);

		final JSpinner spinner_4 = new JSpinner();
		spinner_4.setModel(new SpinnerNumberModel(frameworkVO.getChooseLow(),
				0, 50, 1));
		spinner_4.setToolTipText("输入下限");
		spinner_4.setForeground(Color.GRAY);
		spinner_4.setFont(new Font("微软雅黑", Font.BOLD, 14));
		spinner_4.setBounds(340, 233, 78, 28);
		add(spinner_4);

		final JSpinner spinner_5 = new JSpinner();
		spinner_5.setModel(new SpinnerNumberModel(frameworkVO.getChooseHigh(),
				0, 50, 1));
		spinner_5.setToolTipText("输入上限");
		spinner_5.setForeground(Color.GRAY);
		spinner_5.setFont(new Font("微软雅黑", Font.BOLD, 14));
		spinner_5.setBounds(520, 233, 78, 28);
		add(spinner_5);

		JButton btnNewButton = new JButton("完成");
		btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 13));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(null, "系统将保存信息，是否确认？",
						"确认", JOptionPane.YES_NO_OPTION);
				if (i == JOptionPane.YES_OPTION) {
					int compulsoryLow; // 通识通修
					int compulsoryHigh;
					int chooseLow; // 开放选修
					int chooseHigh;
					int coreLow;
					int coreHigh;
					compulsoryLow = Integer.parseInt(spinner.getValue()
							.toString());
					System.out.println(compulsoryLow);
					compulsoryHigh = Integer.parseInt(spinner_1.getValue()
							.toString());
					coreLow = Integer.parseInt(spinner_2.getValue().toString());
					coreHigh = Integer
							.parseInt(spinner_3.getValue().toString());
					chooseLow = Integer.parseInt(spinner_4.getValue()
							.toString());
					chooseHigh = Integer.parseInt(spinner_5.getValue()
							.toString());
					if (compulsoryLow > compulsoryHigh
							|| chooseLow > chooseHigh || coreLow > coreHigh) {
						JOptionPane.showMessageDialog(null, "请注意，填写的学分下限必须低于学分上限。", "修改失败！",
								JOptionPane.ERROR_MESSAGE);
					} else {
						ResultMessage result = null;
						try {
							result = frameworkController
									.update(new FrameworkVO(compulsoryLow,
											compulsoryHigh, chooseLow,
											chooseHigh, coreLow, coreHigh));
						} catch (Exception e1) {
							// TODO 自动生成的 catch 块
							e1.printStackTrace();
							ExceptionHandler.networkException();
						}
						if (result.equals(ResultMessage.UPDATE_SUCCESS)) {
							JOptionPane.showMessageDialog(null, "修改成功！", "成功！",
									JOptionPane.INFORMATION_MESSAGE);
						} else {
							JOptionPane.showMessageDialog(null, "修改失败！", "失败！",
									JOptionPane.ERROR_MESSAGE);
						}
					}
				} else if (i == JOptionPane.NO_OPTION) {

				} else if (i == JOptionPane.CANCEL_OPTION) {

				}

			}
		});
		btnNewButton.setBounds(340, 331, 83, 35);
		add(btnNewButton);

		JLabel label_3 = new JLabel("学分下限");
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 18));
		label_3.setBounds(339, 41, 78, 28);
		add(label_3);

		JLabel label_6 = new JLabel("学分上限");
		label_6.setFont(new Font("微软雅黑", Font.BOLD, 18));
		label_6.setBounds(520, 41, 78, 28);
		add(label_6);

	}

	public void paintComponent(Graphics g) {
		g.drawImage(INFO, 0, 0, null);
	}
}
